<template>
	<view class="circleDetails">
		<uni-header :title="'圈子'"></uni-header>
		<view class="circle">
			<uni-forms ref="customForm" :modelValue="formData" label-width="85px">
				<uni-forms-item label="圈子头像" name="accessory">
					<uni-file-picker :value="fileLists" fileMediatype="image" @select="onUpload"
						limit="1"></uni-file-picker>
				</uni-forms-item>
				<uni-forms-item label="圈子名称" name="title">
					<uni-easyinput v-model="formData.title" placeholder="请输入圈子名称" />
				</uni-forms-item>
				<uni-forms-item label="学科" name="subject">
					<uni-data-select v-model="formData.subject" :localdata="subject"
						placeholder="请选择学科"></uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="邀请成员" name="group_user">
					<school :selectType="'3'" :text="'邀请成员'" @onSelect="getSelectTeacher"></school>
				</uni-forms-item>
				<uni-forms-item label="圈子内容" name="content">
					<uni-easyinput v-model="formData.content" type="textarea" placeholder="请输入圈子内容" />
				</uni-forms-item>
				<uni-forms-item><u-button @click="onSubmit" type="primary">保存</u-button></uni-forms-item>
			</uni-forms>
		</view>
	</view>
</template>

<script>
	import school from "@/pages/common/school/index"
	import {
		getCircleDetails,
		addNewMember
	} from "@/api/work"
	import config from '@/config'
	const baseUrl = config.baseUrl
	export default {
		data() {
			return {
				imgUrl: config.imgUrl,
				fileLists: [],
				subject: this.$store.state.teach.subject,
				groupUser: [],
				id: "",
				deviceId: this.$store.state.user.deviceId,
				formData: {
					circleId: "",
					accessory: "",
					title: "",
					subject: "",
					group_user: [],
					content: "",
					havepic: "-1"
				}
			}
		},
		onLoad(option) {
			this.id = option.themeId;
			this.formData.circleId = option.themeId;
			this.getCircleDetailsData();
		},
		components: {
			school
		},
		methods: {
			getCircleDetailsData() {
				getCircleDetails({
					circleId: this.id,
					deviceId: this.deviceId
				}).then(res => {
					this.formData.subject = parseInt(res.record.subject)
					this.formData.title = res.record.title
					this.formData.content = res.record.content;
					this.fileLists.push({
						url: this.imgUrl + res.record.accessory,
						extname: 'png',
						name: 'shuijiao.png'
					})
				})
			},
			getSelectTeacher(data) {
				let params = {
					circleId: this.formData.circleId,
					group_user: data.teacher,
					deviceId: this.deviceId
				}
				addNewMember(params).then(res => {
					console.log(res)
				})
				// this.formData.group_user = data.teacher
			},
			onUpload(e) {
				this.formData.havepic = "1"
				this.formData.accessory = e.tempFilePaths[0];
			},
			onSubmit() {
				uni.showLoading({
					title: '正在保存修改'
				})
				uni.uploadFile({
					url: baseUrl + '/circle/updateCircle',
					filePath: this.formData.accessory,
					name: 'file',
					formData: this.formData,
					success: (res) => {
						if (res.statusCode === 200) {
							uni.$u.toast('保存成功');
							setTimeout(() => {
								this.$tab.reLaunch('/pages/work/index')
							}, 2000);
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.circle {
		background: #fff;
		padding: 15px;
	}
</style>